﻿<?php

	defined('_JEXEC') or die('Restricted access');
	require_once(CUTEENGINE_BASE_COMPONENTS_PATH."base_display_component_class.php");
    require_once(CUTEENGINE_NAMESPACES_PATH."javascript_namespace.php");



    //

    // t_html_timer
    //
	class t_html_timer
	{	
		// property
		// public
		// A
		public $autostart = false;
		// I
		public $interval = 0;
		public $interval_first = -1;
		// private
		// C
		private $components_names = array();

		
		// N
		public $name = '';
		
		// method
		// private 
		private function create_javascript_initialization_component()
		{   
            
            global $g_javascript_namespace;
            $g_javascript_namespace->header_script_generator->add_class(
				get_class($this),'a_cuteengine,a_name,a_parent_componet_name,a_log_componet_name,a_component_id,a_events_array, a_autostart, a_interval,a_interval_first, a_components',t_base_display_component::base_javascript_component_name());
				
			
			// properties
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'name',"a_name",false); 	
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'interval',"a_interval",false);	
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'interval_first',"a_interval_first",false);				
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'components',"a_components",false); 
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'executing',"false",false); 
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'last_started_process_no',"-1",false); 	
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'stop_booked',"false",false); 	
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'stoped_with_error',"false",false); 			
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'first_time',"true",false);
			// mothods
			// O
			$local_content = $g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(	
				get_class($this),'on_error','a_req',"
				this.stoped_with_error = true;
				var local_process = a_req.currentTarget.content_loader.owner;
				var local_name = 'no name';
				local_process.on_error();
			");
			// R
            $g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
                get_class($this),'retrive_timer_function','',"
        
		if ((this.first_time) && (this.interval_first > -1))
		{
			this.first_time = false;
			clearInterval(this.timer_function);
            this.timer_function = setInterval(this.retrive_timer_function.set_scope(this), this.interval);
		}
		this.executing = true;
		this.stoped_with_error = false;
		
		var local_start = 0;
		for (var i = local_start; i < this.components.length; i++) 
		{
			var local_process = window.cuteengine[this.components[i]];
			if ((local_process != undefined) && (local_process.is_to_execute()) )
			{
				this.last_started_process_no = i;
				local_process.execute_process(this.on_error.set_scope(this));
			}	
			
		}
		
		");			
			// S
			$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
                get_class($this),'stop_executing','',"
        this.last_started_process_no--;	
        ");	
			$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
                get_class($this),'start_timer','',"
        if ((this.timer_function === null) || (this.timer_function === undefined) )
        { 
			if ((this.first_time) && (this.interval_first > -1))
			{
				this.timer_function = setInterval(this.retrive_timer_function.set_scope(this), this.interval_first);
			}
			else
			{
				this.timer_function = setInterval(this.retrive_timer_function.set_scope(this), this.interval);
			}
			this.stop_booked = false;
        }");	
			$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
                get_class($this),'stop_timer','',"
        if (this.executing)
		{
			this.stop_booked = true;
		}
		else if (this.timer_function !== null) 
        { 
			clearInterval(this.timer_function);
            this.timer_function = null;
        }");	
			$g_javascript_namespace->header_script_generator->add_content_to_a_method(
				get_class($this),t_header_script_generator::create_label,"
				this.start_timer();
				");			
		
		}
		// public
		public function __construct($a_name,$a_autostart,$a_interval,$a_interval_first = -1) 
		{
			$this->name = $a_name;
			$this->autostart = $a_autostart;
			$this->interval = $a_interval;
			$this->interval_first = $a_interval_first;
			//echo "this->autostart: $this->autostart<br>";
            $this->create_javascript_initialization_component();
		}	
		
		
		// A
		public function add_components_name($a_name)
		{
			$this->components_names[] = $a_name;
		}
		// G
		public function get_js_component_array()
		{
			return '["'.implode('","', $this->components_names) .'"]' ;
		}

	}
	
    // t_html_page
    //
	class t_html_page extends t_base_display_component
	{	

		// property
		// public
		// G
		const language_position_last = 1;
		// M
        const main_query_component_content_type = "Content-Type";
        const main_query_component_robots = "robots";
        const main_query_component_site_description = "site_description";
		const main_query_component_title = "title";
        const main_query_component_keywords = "keywords";
        const main_query_component_url_addr = "url_addr";

		

        // H
        public $head_tag = false;

		// B
		public $body_tag = false;
        // S
        const specific_parameters_position_content_type = 1;
        const specific_parameters_position_robots = 2;
        const specific_parameters_position_site_description = 3;
		const specific_parameters_position_title = 4;
        const specific_parameters_position_keywords = 5;
        const specific_parameters_position_url_addr = 6;		
        const specific_parameters_position_last = 7;
        // T

        public $title_tag = false;

		// method
		// protected
		
		// C
		protected function create_tags_structure()

		{   
			global $g_ambient_namespace;
            global $g_javascript_namespace;                
			global $g_database_namespace;
			global $g_url_parameters_namespace;

            //
            $local_url_addr = $this->get_main_query_parameter_value(self::main_query_component_url_addr);    
            $local_content_type = $this->get_main_query_parameter_value(self::main_query_component_content_type);    
            $local_site_description = $this->get_main_query_parameter_value(self::main_query_component_site_description);    
            $local_robots = $this->get_main_query_parameter_value(self::main_query_component_robots);
			// keyword
			
			$local_keywords = $this->get_main_query_parameter_value(self::main_query_component_keywords);// 
			$local_keywords = $g_url_parameters_namespace->add_argument_value_to_str(0,$local_keywords, 4);
			$local_keywords = preg_replace('/\s+/', ' ',$local_keywords);
			$local_keywords = preg_replace('/[,\s]{2,}/', ', ',$local_keywords);
			
			//title			
			$local_title = $this->get_main_query_parameter_value(self::main_query_component_title);
            //
           /* echo "local_url_addr = $local_url_addr;    
                local_content_type = $local_content_type;    
                local_site_description = $local_site_description;    
                local_robots = $local_robots;
                local_keywords = $local_keywords;			
                local_title = $local_title;";*/
            //

            $g_javascript_namespace->header_script_generator->set_title($local_title);

            //

            if (! $this->main_tag)

			{

				$this->main_tag = new t_base_tag(t_base_tag_type::btt_doctype,'doctype');
                $local_header_script_generator = $this->main_tag->header_script_generator;
				//<html lang="it" xml:lang="it" xmlns="http://www.w3.org/1999/xhtml">
				//echo "HHHHHTTTTTTMMMLLLLL";
                $local_html_tag = $this->main_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_html,$this->name);
				if ($g_ambient_namespace->edit_mode == true)
				{
					//echo "ciao";
				}
				$local_html_tag->add_property('lang','it');
				$local_html_tag->add_property('xml:lang','it');
				$local_html_tag->add_property('xmlns','http://www.w3.org/1999/xhtml');
				//<head>
				$this->head_tag = $local_html_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_head,$this->name.'_head');
                // <meta>
                $g_javascript_namespace->header_script_generator->add_meta_tag('author','',$local_url_addr);
                $g_javascript_namespace->header_script_generator->add_meta_tag('','content-type',$local_content_type);
                $g_javascript_namespace->header_script_generator->add_meta_tag('description','',$local_site_description);
                $g_javascript_namespace->header_script_generator->add_meta_tag('robots','',$local_robots);
                $g_javascript_namespace->header_script_generator->add_meta_tag('keywords','',$local_keywords);
				$g_javascript_namespace->header_script_generator->add_meta_tag('title','',$local_title);
				$g_javascript_namespace->header_script_generator->add_meta_tag('viewport','',"width=device-width,initial-scale=1");

				// <title>
				$this->title_tag = $this->head_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_title,$this->name.'title');

				//<body>
				$this->body_tag = $local_html_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_body,$this->name.'_body');
                $this->body_tag->add_event_as_local_method("body","onLoad","init_cuteengine()",false,'',
					true,true);
				$this->body_tag->include_child_str = true;

                //
                $local_header_script_generator->add_style_sheet_property(
					"body",'background-color','#FFF9F0');
                $local_header_script_generator->add_style_sheet_property(
					"body",'color','#786953');
                $local_header_script_generator->add_style_sheet_property(
					"body",'font-family','Tahoma,Geneva,Verdana');
                $local_header_script_generator->add_style_sheet_property(
					"body",'font-size','100%');
                //
                $this->on_create_main_tag();
	            
				/*$this->body_tag->add_text_to_include("<div id=\"fb-root\"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = \"//connect.facebook.net/it_IT/all.js#xfbml=1\";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>");*/
                // javascript google_analytics_script
				if ($g_database_namespace->google_analytics_enable)
				{
					$this->body_tag->add_text_to_include('<script type="text/javascript">' . $g_database_namespace->google_analytics_script . '</script>');
					$this->body_tag->add_text_to_include('<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/it_IT/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, \'script\', \'facebook-jssdk\'));</script>');
				}				

		

			}

		}

		// G
		public function get_language_position_last()
        {
            return self::language_position_last;
        }
        static function get_help_specific_parameter($a_index)
		{
			$local_return = 'NO HELP AVAILABLE';
			/*switch ($a_index) 
            {
                case self::specific_parameters_position_:
					$local_return = '';
				break;                
			}*/
			return $local_return;
		}
        static function get_default_specific_parameter($a_index)
		{
			$local_return = '';
			//echo "a_index: $a_index<br>";
			switch ($a_index) 
            {
                /*case self::specific_parameters_position_:
					$local_return = 'TRUE';
				break;                */
			}
			return $local_return;
		}	
        protected function get_specific_parameters_position_last()
        {
            return self::specific_parameters_position_last;
        }		
        // private
		// I
		protected function initialize_query_parameters()
		{
			// db array parameter								
			$this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_url_addr,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				1);	

            $this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_title,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				2);		

            $this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_content_type,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				3);		

            $this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_site_description,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				4);		

            $this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_robots,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				5);		

            $this->new_query_parameter(
				t_cuteengine_component::main_query_parameter_name,
				self::main_query_component_keywords,
				t_cuteengine_component::main_query_field_name_specific_parameters,
				6);			
		}
		// public

		public function __construct($a_cuteengine_component,$a_query_parameter) 

		{
			//$this->create_javascript_component = false;
            parent::__construct($a_cuteengine_component,$a_query_parameter);
		}	

		//D

	/*	public function display_component()

		{

			parent::display_component();

		}*/

		// E

		public function execute_component_action()

		{

			$local_function = parent::execute_component_action();

            if ($local_function)

			{

				$this->$local_function();

            }

			else

			{

				 

			}

			//echo "argomento: ".$this->url_argument(self::main_url_argument_import_file_name);

			//$this->parse_source();

		}

		// G

		public function generate_html_code($a_child_str_to_include = '')

		{

			global $g_javascript_namespace;

			//$local_body_events_array = $g_javascript_namespace->generate_body_events_array();

			//$local_body_events_array = $g_javascript_namespace->javascript_body_events;

			//$local_body_events_delayed_array = $g_javascript_namespace->javascript_body_events_delayed;

			//$this->body_tag->import_events($local_body_events_array);

			//

            $local_head_tag = $this->head_tag;

            //echo $local_head_tag->name;

            $g_javascript_namespace->header_script_generator->add_meta_tags_to_head_tag($local_head_tag);

            $this->title_tag->set_text_to_include($g_javascript_namespace->header_script_generator->get_title());

            

            $local_return = parent::generate_html_code($a_child_str_to_include,

				$local_body_events_html_code);

			//

			

			/*$local_body_events_html_code = $g_javascript_namespace->generate_body_events_html_code();

			$local_return = str_replace($this->get_start_tag($a_base_tag_type),"",$local_cookie);*/

			//

			return $local_return;	

		}	

        // R

   		public function reset_head_content()

		{

            //$this->head_tag->set_text_to_include('');

            $this->head_tag->clear_text_to_include_for_all_childs();

		}        



        // S

		public function set_external_link_script()

		{
			//echo __METHOD__;
            global $g_javascript_namespace;

            $local_script = '';

            $g_javascript_namespace->header_script_generator->generate_external_link_text_script($local_script);

            echo "base_tag_type: ".$this->head_tag->base_tag_type." -  local_script: $local_script <br>";

            

            $this->head_tag->add_text_to_include($local_script);

		}     

		public function include_external_link_text_script()

		{
            global $g_javascript_namespace;
            try
            {
                //echo __METHOD__; 

                $local_script = '';

                $g_javascript_namespace->header_script_generator->include_external_link_text_script($local_script);

                //echo "base_tag_type: ".$this->head_tag->base_tag_type." -  local_script: $local_script";

                //echo "local_script:$local_script <br>";

                $this->set_style_sheet_script($local_script);
            }    
            catch (Exception $e) 
            {
                //echo "<BR>" . __METHOD__ . "ERRORE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!: ".$e->getMessage()."<BR>";
                $this->log->add_log_error($e);
            }                 

		}     

        

        

		public function set_style_sheet_script($style_sheet_script)

		{

           // echo "style_sheet_script:" .$style_sheet_script ."<br>";
            try
            {
                //echo __METHOD__;
                $local_name = "style_sheet";

                if (! $this->head_tag->is_there_child_by_name($local_name))

                    $this->head_tag->create_and_add_child_at_top(t_base_tag_type::btt_style_sheet,$local_name);

                $this->head_tag->add_text_to_include_by_name($local_name,$style_sheet_script);
            }
            catch (Exception $e) 
            {
                //echo "<BR>" . __METHOD__ . "ERRORE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!: ".$e->getMessage()."<BR>";
                $this->log->add_log_error($e);
            } 

		}         

		public function set_javascript_functions_script($javascript_functions_script)

		{
           // echo "javascript_functions_script:$javascript_functions_script<br>";

            try
            {
                $local_name = "javascript_functions_script";

                if (! $this->head_tag->is_there_child_by_name($local_name))

                {
                    //echo "<br>javascript_functions_script NON ESISTE!!<br>";
                    // creates and add to the top on the tags list the tag javascript on the tag  $this->head_tag with name "javascript_functions_script"

                    $this->head_tag->create_and_add_child_at_top(t_base_tag_type::btt_javascript_script,$local_name);

                }

                //

                //global $g_javascript_namespace;

                //$local_body_javascript_functions_script = $g_javascript_namespace->generate_body_javascript_functions_script();

                //

                // Save on "javascript_functions_script" tag the text $local_body_javascript_functions_script.$javascript_functions_script

                $this->head_tag->add_text_to_include_by_name($local_name,/*$local_body_javascript_functions_script.*/$javascript_functions_script);
            }
            catch (Exception $e) 
            {
                //echo "<BR>" . __METHOD__ . "ERRORE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!: ".$e->getMessage()."<BR>";
                $this->log->add_log_error($e);
            } 
                

		}        



	}

?>